//
//  ViewController.m
//  WebJS
//
//  Created by liuxiang on 2017/12/6.
//  Copyright © 2017年 liuxiang. All rights reserved.
//

#import "ViewController.h"
#import "JSExportMessage.h"


@interface ViewController () <UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (nonatomic, strong) JSContext *context;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSURL *htmlURL = [[NSBundle mainBundle] URLForResource:@"1" withExtension:@"html"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:htmlURL];
    request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    [self.webView loadRequest:request];
    
}
- (IBAction)oneClick:(id)sender {
    // 调用js sendMsssageToOC 方法
    [self.context evaluateScript:@"sendMsssageToOC('执行js方法')"];
}

- (IBAction)twoClick:(id)sender {
    
}

- (IBAction)threeClick:(id)sender {
    
}

- (IBAction)reloadWebView:(id)sender {
    [self.webView reload];
}

- (void) webViewDidFinishLoad:(UIWebView *)webView {
    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    self.navigationItem.title = title;
    // 执行HTML中的js方法
//    NSString *hello = [webView stringByEvaluatingJavaScriptFromString:@"hello(\"hello world\")"];
//    NSLog(@"hello result = %@",hello);
    
    JSContext * context =[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.context = context;
    //直接执行js代码
//    [context evaluateScript:@"alert('context evaluateScript')"];
    /*
    //js 使用oc传递的参数
    context[@"sendMsssageToOC"] = ^{
        // 执行js方法的所有参数
        NSArray *args = [JSContext currentArguments];
        for (id obj in args) {
            NSLog(@"args = %@",obj);
        }
    };
    // 调用 js sendMsssageToOC 方法
    JSValue *value = context[@"sendMsssageToOC"];
    JSValue *result = [value callWithArguments:@[@"hahah"]];
    NSLog(@"excute result = %@",[result toString]);
    */
    
    JSExportMessage *export = [[JSExportMessage alloc] init];
    export.name =  @"exportname";
    export.desc = [export description];
    // 把对象注入js, js中就可以通过该对象调用方法，执行oc类中的同名方法
    context[@"jsexport"] = export;
    
    // 模拟js调用 代码
//   JSValue *result1 = [context evaluateScript:@"jsexport.sendMsssageToJS('12312412')"];
//    NSLog(@"excute result1 = %@",[result1 toString]);
    

    
}

- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *url = request.URL.absoluteString;
//    com.taylor.custom:
    if ([url hasPrefix:@"com.taylor.custom://"]) {
        //自定义的URL拦截，执行特殊操作
        // 获取参数列表
        NSArray *array = [url componentsSeparatedByString:@"//"];
        NSArray *paraArray = [array.lastObject componentsSeparatedByString:@"&"];
        UIViewController *customVC = [[UIViewController alloc] init];
//        customVC.a = paraArray.firstObject;
//        customVC.b = paraArray.lastObject;
        customVC.view.backgroundColor = [UIColor whiteColor];
        [self.navigationController pushViewController:customVC animated:YES];
        
        return NO;
    }
    
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
